//
//  DoomScreenView+Input.m
//  MobileDoom
//
//  Created by Steven Troughton-Smith on 27/07/2008.
//  Copyright 2008 Steven Troughton-Smith. All rights reserved.
//

#import "DoomScreenView+Input.h"
#include "doom/doomdef.h"

BOOL zoomedIn = NO;
static int curWeapon = 0;

@implementation UIDoomScreenView (Input)

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
	if (!event)
		[super touchesBegan:touches withEvent:event];
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
//	if (zoomedIn)
//	{
//		zoomedIn = NO;
//		[eventQueue addObject:[[DoomEvent alloc] initWithKeyDown:KEY_MINUS]];
//		[eventQueue addObject:[[DoomEvent alloc] initWithKeyUp:KEY_MINUS]];
//	}
//	else
//	{
//		zoomedIn = YES;
//		[eventQueue addObject:[[DoomEvent alloc] initWithKeyDown:KEY_EQUALS]];
//		[eventQueue addObject:[[DoomEvent alloc] initWithKeyUp:KEY_EQUALS]];
//	}
	[eventQueue addObject:[[DoomEvent alloc] initWithKeyDown:KEY_ESCAPE]];
	[eventQueue addObject:[[DoomEvent alloc] initWithKeyUp:KEY_ESCAPE]];

}

- (void)willMoveToWindow:(UIWindow *)newWindow
{
	eventQueue = [[[NSMutableArray alloc] init] retain];

	[[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / kUpdateFrequency)];
	[[UIAccelerometer sharedAccelerometer] setDelegate:self];
}

- (void)endTouch:(id)sender
{
#if 0
	if (lastKey)
	{
		[eventQueue addObject:[[DoomEvent alloc] initWithKeyUp:lastKey]];
		lastKey = 0;
	}
#endif
}

-(void)buttonDown:(id)sender
{
	int key = -1;
	
	switch ([sender tag])
	{
			
		case 1:
			key = KEY_UPARROW;
			break;
			
		case 2:
			key = KEY_DOWNARROW;
			break;
			
		case 3:
			key = KEY_LEFTARROW;
			break;
			
		case 4:
			key = KEY_RIGHTARROW;
			break;
			
		case 5:
			key = KEY_RCTRL;
			break;
			
		case 6:
			key = 32;
			break;

		case 8:
			key = KEY_F9;
			break;
			
		case 9:
			key = KEY_F6;
			break;

		case 10:
			key = 'y';
			break;
			
		case 11:
			key = 'n';
			break;
				
		case 12:
			curWeapon++;
			if(curWeapon > 8)
				curWeapon = 0;
			key = '1' + curWeapon;
			break;

		default:
			break;
			
	}
#if 0
	lastKey = key;

	if (key == KEY_LEFTARROW)
		[eventQueue addObject:[[DoomEvent alloc] initWithKeyUp:KEY_RIGHTARROW]];

	if (key == KEY_RIGHTARROW)
		[eventQueue addObject:[[DoomEvent alloc] initWithKeyUp:KEY_LEFTARROW]];

	if (key != KEY_RCTRL)
		[eventQueue addObject:[[DoomEvent alloc] initWithKeyUp:KEY_RCTRL]];
	if (key != 32)
		[eventQueue addObject:[[DoomEvent alloc] initWithKeyUp:32]];
	
	[eventQueue addObject:[[DoomEvent alloc] initWithKeyUp:KEY_LEFTARROW]];
#endif
	
	if (key != KEY_RCTRL)
		[eventQueue addObject:[[DoomEvent alloc] initWithKeyUp:KEY_RCTRL]];
	if (key != 32)
		[eventQueue addObject:[[DoomEvent alloc] initWithKeyUp:32]];	
	if (key != KEY_LEFTARROW)
		[eventQueue addObject:[[DoomEvent alloc] initWithKeyUp:KEY_LEFTARROW]];
	if (key != KEY_RIGHTARROW)
		[eventQueue addObject:[[DoomEvent alloc] initWithKeyUp:KEY_RIGHTARROW]];
	if (key < '1' && key > '9')
		[eventQueue addObject:[[DoomEvent alloc] initWithKeyUp:('1' + (curWeapon == 0 ? 8 : curWeapon - 1))]];
	
	if(key >= 0)
		[eventQueue addObject:[[DoomEvent alloc] initWithKeyDown:key]];
}

-(void)buttonUp:(id)sender
{
	int key = -1;
	
	switch ([sender tag])
	{			
		case 1:
			key = KEY_UPARROW;
			break;
			
		case 2:
			key = KEY_DOWNARROW;
			break;
			
		case 3:
			key = KEY_LEFTARROW;
			break;
			
		case 4:
			key = KEY_RIGHTARROW;
			break;
			
		case 5:
			key = KEY_RCTRL;
			break;
			
		case 6:
			key = 32;
			break;
			
		case 8:
			key = KEY_F9;
			break;
			
		case 9:
			key = KEY_F6;
			break;
			
		case 10:
			key = 'y';
			break;
			
		case 11:
			key = 'n';
			break;
			
		case 12:
			key = '1' + (curWeapon == 0 ? 8 : curWeapon - 1);
			break;			
			
		case 7:
		{
			if ([self isFirstResponder])
				[self resignFirstResponder];

				else
				[self becomeFirstResponder];
			
		}
		default:
			break;
			
	}
	
	if(key == KEY_LEFTARROW)
		[eventQueue addObject:[[DoomEvent alloc] initWithKeyUp:KEY_RIGHTARROW]];
	else if(key == KEY_RIGHTARROW)
		[eventQueue addObject:[[DoomEvent alloc] initWithKeyUp:KEY_LEFTARROW]];
	
	if(key >= 0)
		[eventQueue addObject:[[DoomEvent alloc] initWithKeyUp:key]];
}

//NSLock *curLock;

- (int)getNextEvent:(event_t*)event
{
	const  int maxEvents = 64;
	static int eventIndex = 0;
	
	if ([eventQueue count] < eventIndex+1)
		return 0;
	
	NSAutoreleasePool *pool = [NSAutoreleasePool new];
	//
//	if (!curLock)
//		curLock = [NSLock new];
//	
//	[curLock lock];
	DoomEvent* doomEvent = [eventQueue objectAtIndex:eventIndex];
	*event = [doomEvent event];
	
	//[curLock unlock];
	
	//  if (event->type != ev_mouse)
	//    NSLog(@"event %d %d %d %d",
	//       event->type, event->data1, event->data2, event->data3);
	
	
	[pool release];
	
	eventIndex++;
	
	if(eventIndex >= maxEvents)
	{
		int i;
		for(i=0; i<[eventQueue count]; i++)
		{
			DoomEvent* theEvent = [eventQueue objectAtIndex:i];
			[theEvent release];
		}
		// If a new event occurs right here then that memory and event leaks!
		[eventQueue removeAllObjects];
		eventIndex = 0;
	}
	return 1;
}

#pragma mark -
#pragma mark === Responding to accelerations ===
#pragma mark -
// UIAccelerometer delegate method, which delivers the latest acceleration data.
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
    // Use a basic low-pass filter to only keep the gravity in the accelerometer values for the X and Y axes
    accelerationX = acceleration.x * kFilteringFactor + accelerationX * (1.0 - kFilteringFactor);
    accelerationY = acceleration.y * kFilteringFactor + accelerationY * (1.0 - kFilteringFactor);
    accelerationZ = acceleration.z * kFilteringFactor + accelerationZ * (1.0 - kFilteringFactor);
    
	float forwardrotation = accelerationX;
	float siderotation = accelerationY;
	int speed = 0;
	/*
	static float staticx = 0.0;
	static float staticy = 0.0;
	if(staticx != siderotation || staticy != forwardrotation)
		NSLog(@"x: %f y: %f", siderotation, forwardrotation);
	staticx = siderotation;
	staticy = forwardrotation;
	*/
	
	if(forwardrotation > -0.75f && forwardrotation < -0.15f)
	{
		if(forwardrotation < -0.45)
		{
			mouseY = 50;
			speed = 1;
		}
		else
		{
			mouseY = 25;
		}
	}
	else if(forwardrotation > 0.15f && forwardrotation < 0.75f)
	{
		if(forwardrotation > 0.45)
		{
			mouseY = -50;
			speed = 1;
		}
		else
		{
			mouseY = -25;
		}
	}
	else
	{
		mouseY = 0;
	}
/*
	if (siderotation > 0.15f)	
	{
		mouseX = -80;
	}
	else if (siderotation < -0.15f)
	{
		mouseX = 80;
	}
	else
	{
		mouseX = 0;
	}
*/
	// ZodTTD Edit: Check eventQueue exists
	if(eventQueue != nil)
	{
		if(speed)
		{
			[eventQueue addObject:[[DoomEvent alloc] initWithKeyDown:KEY_RSHIFT]];
		}
		else
		{
			[eventQueue addObject:[[DoomEvent alloc] initWithKeyUp:KEY_RSHIFT]];
		}
		[eventQueue addObject:[[DoomEvent alloc] initWithMouseX:0 Y:mouseY]];
	}
}


@end

